<?php
namespace App\Http\Controllers\Friend;
use App\Http\Classes\FriendGroup;
use App\Http\Controllers\BaseController;
use App\Http\Classes\Log;
use Illuminate\Http\Request;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/25 0025
 * Time: 下午 2:11
 */
class FriendGroupController extends BaseController
{
    public function getUserFriendGroups(){
        try{
            $groups = FriendGroup::getUserAllGroups($_COOKIE['user_id']);
            if($groups === null){
                return $this->response(304,'没有分组',null);
            }
            else{
                return $this->response(200,'获取成功',$groups);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    public function addFriendGroup(Request $request){
        try{
            if($request->has('group_name')){
                $groupName = $request->input('group_name');
                if($groupName == '' || $groupName == null){
                    return $this->response(304,'分组名称不能为空！',$groupName);
                }
                else{
                    $data = array();
                    $data['group_user_id'] = $_COOKIE['user_id'];
                    $data['group_name'] = $groupName;
                    if(FriendGroup::isGroupNameExists($data)){
                        return $this->response(304,'分组名称已经存在！',$groupName);
                    }
                    else{
                        $group = FriendGroup::addGroup($data);
                        if($group === null){
                            return $this->response(304,'添加成功！获取失败',$group);
                        }
                        elseif ($group === false){
                            return $this->response(304,'添加失败！',$group);
                        }
                        else{
                            return $this->response(200,'添加成功！',$group);
                        }
                    }
                }
            }
            else{
                return $this->response(503,'缺少必要参数group_name',null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }
}